package com.kuns.subject.domian.handler.subject;

import com.kuns.subject.common.enums.IsDeletedFlagEnum;
import com.kuns.subject.common.enums.SubjectInfoTypeEnum;
import com.kuns.subject.domian.convert.SubjectRadioConverter;
import com.kuns.subject.domian.entity.SubjectAnswerBO;
import com.kuns.subject.domian.entity.SubjectInfoBO;
import com.kuns.subject.domian.entity.SubjectOptionBO;
import com.kuns.subject.infra.basic.entity.SubjectRadio;
import com.kuns.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> subjectRadioList = new ArrayList<>();
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = SubjectRadioConverter.INSTANCE.convertBoTiEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO queryOption(Long subjectId) {
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryList(subjectId);
        List<SubjectAnswerBO> subjectAnswerBOList = SubjectRadioConverter.INSTANCE.convertEntityListToBoList(subjectRadioList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
